<template>
  <div>
    <van-nav-bar
      title="收款详情"
      left-arrow
      left-text="返回"
      @click-left="$emit('close')"
    >
      <template #right>
        <van-popover
          v-model="showPopover"
          placement="bottom-end"
          trigger="click"
          :actions="actions"
          @select="handleAction"
        >
          <template #reference>
            <van-icon name="ellipsis" size="20" />
          </template>
        </van-popover>
      </template>
    </van-nav-bar>
    <van-cell-group>
      <van-cell title="收款单号" :value="submitForm.receiptNo" />
      <van-cell title="收款日期" :value="submitForm.receiptDate" />
      <van-cell title="客户" :value="submitForm.customerName" />
      <van-cell title="收款方式">
        <template #default>
          <dict-value v-model="submitForm.receiptMethod" dict-key="payment_method" />
        </template>
      </van-cell>
      <van-cell title="订单号" :value="submitForm.orderNo" />
    </van-cell-group>
    <van-row v-if="orderInfo.id">
      <van-cell-group inset>
        <van-cell>
          <template #title>
            <span>{{ orderInfo.productName }}</span>
          </template>
          <template #default>
            <span>{{ orderInfo.quantity }}{{ orderInfo.unit }}</span>
          </template>
        </van-cell>
        <van-cell title="总金额" :value="orderInfo.amount">
          <template #label>
            <span>已付款{{ orderInfo.paidAmount }}</span>
          </template>
        </van-cell>
        <van-cell title="剩余待付款" :value="orderInfo.remainingAmount">
          <template #default>
            <span class="red">{{ orderInfo.remainingAmount }}</span>
          </template>
        </van-cell>
        <van-cell title="收款金额" :value="submitForm.amount" />
        <van-cell title="备注" :value="submitForm.remark" />
      </van-cell-group>
    </van-row>
    <van-popup v-model="showSubmit" position="bottom" :style="{ height: '100%' }">
      <receipt-submit-module ref="submitModule" @close="handleClose" />
    </van-popup>
  </div>
</template>

<script>
import { deleteReceipt, selectReceiptById } from '@/api/receipt'
import { selectSaleOrderById } from '@/api/saleOrder'
import ReceiptSubmitModule from '@/views/receipt/receiptSubmit.vue'
import { Dialog } from 'vant'
import DictValue from '@/components/dictValue.vue'

export default {
  name: 'ReceiptDetailModule',
  components: { DictValue, ReceiptSubmitModule },
  data() {
    return {
      showPopover: false,
      showSubmit: false,
      submitForm: {},
      orderInfo: {},
      actions: [{ icon: 'edit', text: '修改', action: 'edit', disabled: false },
        { icon: 'delete-o', text: '删除', action: 'delete', disabled: false }]
    }
  },
  methods: {
    init(id) {
      selectReceiptById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      })
    },
    initOrder(orderId) {
      selectSaleOrderById(orderId).then(resp => {
        if (resp.success) {
          this.orderInfo = resp.data
          this.orderInfo.unit = resp.data.productInfo.unit
        }
      })
    },
    handleAction(row) {
      if (row.action === 'delete') {
        this.handleDelete()
      } else if (row.action === 'edit') {
        this.toEdit()
      }
    },
    handleDelete() {
      Dialog.confirm({
        title: '删除确认',
        message: '是否删除此收款信息'
      }).then(() => {
        deleteReceipt({ ids: [this.submitForm.id] }).then(resp => {
          if (resp.success) {
            this.$toast.success('删除成功')
            this.$emit('close')
          }
        })
      }).catch(() => {})
    },
    toEdit() {
      this.showSubmit = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(this.submitForm.id)
        this.$refs.submitModule.initOrder(this.submitForm.orderId)
      })
    },
    handleClose() {
      this.showSubmit = false
      this.init(this.submitForm.id)
      this.initOrder(this.submitForm.orderId)
    }
  }
}
</script>

<style scoped>

</style>
